<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"llmysnow.top","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="Vue 原理 其他扩展：vue面试中常见的面试题  原理的意义  知其然知其所以然——各行业通用的道理 了解原理，才能应用的更好 大厂造轮子（业务定制、技术 KPI）">
<meta property="og:type" content="article">
<meta property="og:title" content="Vue 原理（虚拟DOM、diff算法、模板编译）">
<meta property="og:url" content="http://llmysnow.top/posts/65e0f6f9/index.html">
<meta property="og:site_name" content="奇幻的梦之旅">
<meta property="og:description" content="Vue 原理 其他扩展：vue面试中常见的面试题  原理的意义  知其然知其所以然——各行业通用的道理 了解原理，才能应用的更好 大厂造轮子（业务定制、技术 KPI）">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/lilyn/pic/raw/master/jslearn-img/MVVM.png">
<meta property="og:image" content="https://gitee.com/lilyn/pic/raw/master/jslearn-img/diff%E7%AE%97%E6%B3%95%E6%A6%82%E8%BF%B0.png">
<meta property="og:image" content="https://gitee.com/lilyn/pic/raw/master/jslearn-img/diff%E7%AE%97%E6%B3%95%E6%97%B6%E9%97%B4%E5%A4%8D%E6%9D%82%E5%BA%A6.png">
<meta property="og:image" content="https://gitee.com/lilyn/pic/raw/master/jslearn-img/diff%E7%AE%97%E6%B3%95%E6%97%B6%E9%97%B4%E5%A4%8D%E6%9D%82%E5%BA%A62.png">
<meta property="og:image" content="https://gitee.com/lilyn/pic/raw/master/jslearn-img/updateChildren2.png">
<meta property="og:image" content="https://gitee.com/lilyn/pic/raw/master/jslearn-img/updateChildren3.png">
<meta property="og:image" content="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/d84848aa035b4472b4d3f8e4b0d708c5~tplv-k3u1fbpfcp-watermark.awebp">
<meta property="og:image" content="https://gitee.com/lilyn/pic/raw/master/jslearn-img/vue%E6%B8%B2%E6%9F%93%E6%9B%B4%E6%96%B0%E6%B5%81%E7%A8%8B%E5%9B%BE.png">
<meta property="article:published_time" content="2022-04-23T11:27:04.400Z">
<meta property="article:modified_time" content="2022-04-23T11:27:47.148Z">
<meta property="article:author" content="LiLyn">
<meta property="article:tag" content="Vue">
<meta property="article:tag" content="diff算法">
<meta property="article:tag" content="虚拟DOM">
<meta property="article:tag" content="模板编译">
<meta property="article:tag" content="前端路由原理">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/lilyn/pic/raw/master/jslearn-img/MVVM.png">

<link rel="canonical" href="http://llmysnow.top/posts/65e0f6f9/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>Vue 原理（虚拟DOM、diff算法、模板编译） | 奇幻的梦之旅</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="奇幻的梦之旅" type="application/atom+xml">
</head>
<!--即时预加载技术，在用户点击之前预先加载页面-->
<script src="https://cdn.jsdelivr.net/gh/TRHX/CDN-for-itrhx.com@2.0.2/js/instantclick-1.2.2.js" type="module"></script>
<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>
	  <!--<a target="_blank" rel="noopener" href="https://github.com/llwodexue" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#151513; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>-->
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">奇幻的梦之旅</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">http://llmysnow.top/</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://llmysnow.top/posts/65e0f6f9/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/logo.jpg">
      <meta itemprop="name" content="LiLyn">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="奇幻的梦之旅">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Vue 原理（虚拟DOM、diff算法、模板编译）
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2022-04-23 19:27:04" itemprop="dateCreated datePublished" datetime="2022-04-23T19:27:04+08:00">2022-04-23</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Vue/" itemprop="url" rel="index"><span itemprop="name">Vue</span></a>
                </span>
            </span>

          <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>25k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>23 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="Vue-原理"><a href="#Vue-原理" class="headerlink" title="Vue 原理"></a>Vue 原理</h2><blockquote>
<p>其他扩展：<a target="_blank" rel="noopener" href="https://blog.csdn.net/Govern66/article/details/104452545">vue面试中常见的面试题</a></p>
</blockquote>
<p><strong>原理的意义</strong></p>
<ul>
<li>知其然知其所以然——各行业通用的道理</li>
<li>了解原理，才能应用的更好</li>
<li>大厂造轮子（业务定制、技术 KPI）</li>
</ul>
<a id="more"></a>

<p><strong>如何考察</strong></p>
<ul>
<li>考察重点，而不是考察细节。掌握好 2/8 原则</li>
<li>和使用相关联的原理，例如：vdom、模板渲染</li>
<li>整体流程是否全面？热门技术是否有深度？</li>
</ul>
<p><strong>重要的原理</strong></p>
<ul>
<li>组件化</li>
<li>响应式</li>
<li>vdom 和 diff</li>
<li>模板编译</li>
<li>渲染过程</li>
<li>前端路由</li>
</ul>
<p><strong>原理题</strong></p>
<ul>
<li>为何 v-for 中要用 key</li>
<li>描述组件渲染和更新的过程</li>
<li>双向数据绑定 v-model 的实现原理</li>
</ul>
<h2 id="如何理解-MVVM"><a href="#如何理解-MVVM" class="headerlink" title="如何理解 MVVM"></a>如何理解 MVVM</h2><ul>
<li>“很久以前” 的组件化<ul>
<li>asp、jsp、php 已经有组件化了</li>
<li>nodejs 中也有类似的组件化</li>
</ul>
</li>
<li>数据驱动视图（MVVM、setState）<ul>
<li>传统组件，只是静态渲染，更新还要依赖于操作 DOM</li>
<li>数据驱动视图——Vue MVVM</li>
<li>数据驱动视图——React setState</li>
</ul>
</li>
</ul>
<p><img data-src="https://gitee.com/lilyn/pic/raw/master/jslearn-img/MVVM.png"></p>
<h2 id="Vue-响应式（数据拦截）"><a href="#Vue-响应式（数据拦截）" class="headerlink" title="Vue 响应式（数据拦截）"></a>Vue 响应式（数据拦截）</h2><blockquote>
<p>推荐文章：<a target="_blank" rel="noopener" href="https://juejin.cn/post/6963079075938172936">简单手写实现Vue2.x</a></p>
</blockquote>
<ul>
<li>组件 data 数据一旦变化，立刻触发视图的更新</li>
<li>实现数据驱动视图的第一步</li>
</ul>
<h3 id="数据响应式原理"><a href="#数据响应式原理" class="headerlink" title="数据响应式原理"></a>数据响应式原理</h3><blockquote>
<p>源码级别可以参考： <a target="_blank" rel="noopener" href="https://ustbhuangyi.github.io/vue-analysis/v2/reactive/reactive-object.html#object-defineproperty">Vue.js 技术揭秘——深入响应式原理</a></p>
</blockquote>
<p>在 JavaScript 的对象 Object 中有一个属性叫访问器属性，其中有 <code>[[Get]]</code> 和 <code>[[Set]]</code> 特性，它们分别是获取函数和设置函数</p>
<p><strong>核心 API</strong>：<code>Object.defineProperty</code></p>
<ul>
<li><p>存在一些问题，Vue 3.0 启动 <code>Proxy</code></p>
<p><code>Proxy</code> 可以原生支持监听数组变化</p>
<p>但是 <code>Proxy</code> 兼容性不好，且无法 <code>polyfill</code></p>
</li>
</ul>
<p><strong>问题</strong></p>
<ul>
<li>深度监听，需要递归到底，一次性计算量大</li>
<li>无法监听新增属性/删除属性（<code>Vue.set</code>、<code>Vue.delete</code>）</li>
<li>不能监听数组变化（重新定义原型，重写 <code>push</code>、<code>pop</code> 等方法）</li>
</ul>
<blockquote>
<p><code>Object.defineProperty</code> 实现响应式</p>
<ul>
<li><code>observe</code> 的功能：给非 VNode 的对象类型数据添加一个 <code>Observer</code> ，用来监听数据的变化</li>
<li><code>Observer</code> 的作用：给对象的属性添加 getter 和 setter，用来依赖收集和派发更新。对于数组会调用 <code>observeArray</code> 方法，对于对象会对对象的 key 调用 <code>defineReactive</code> 方法</li>
<li><code>defineReactive</code> 的功能：定义一个响应式对象，给对象动态添加 getter 和 setter。对子对象递归调用 <code>observe</code> 方法，这样就保证了无论 <code>obj</code> 的结构多复杂，它的所有子属性也能变成响应式的对象，这样我们访问或修改 <code>obj</code> 中一个嵌套较深的属性，也能触发 getter 和 setter</li>
<li>setter 的时候，会通知所有的订阅者</li>
<li><code>arrayMethods</code> 首先继承了 <code>Array</code>，然后对数组中的所有能改变数组自身的方法进行重写，重写后的方法会先执行它们本身原有的逻辑，然后把新添加的值</li>
</ul>
</blockquote>
<p><strong>简单实现 Vue 中的 defineReactive</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 触发更新视图</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">updateView</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&#x27;视图更新&#x27;</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 重新定义数组原型</span></span><br><span class="line"><span class="keyword">const</span> arrayProto = <span class="built_in">Array</span>.prototype</span><br><span class="line"><span class="comment">// 创建新对象，原型指向 arrayProto ，再扩展新的方法不会影响原型</span></span><br><span class="line"><span class="keyword">const</span> arrayMethods = <span class="built_in">Object</span>.create(arrayProto)</span><br><span class="line">;[<span class="string">&#x27;push&#x27;</span>, <span class="string">&#x27;pop&#x27;</span>, <span class="string">&#x27;shift&#x27;</span>, <span class="string">&#x27;unshift&#x27;</span>, <span class="string">&#x27;splice&#x27;</span>, <span class="string">&#x27;sort&#x27;</span>, <span class="string">&#x27;reverse&#x27;</span>].forEach(<span class="function"><span class="params">method</span> =&gt;</span> &#123;</span><br><span class="line">  arrayMethods[method] = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    arrayProto[method].call(<span class="built_in">this</span>, ...arguments) <span class="comment">// 原始操作</span></span><br><span class="line">    updateView() <span class="comment">// 触发视图更新</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 重新定义属性，监听起来</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">defineReactive</span>(<span class="params">target, key, value</span>) </span>&#123;</span><br><span class="line">  observer(value) <span class="comment">// 进行监听</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">// 不可枚举的不用监听</span></span><br><span class="line">  <span class="keyword">const</span> property = <span class="built_in">Object</span>.getOwnPropertyDescriptor(target, key)</span><br><span class="line">  <span class="keyword">if</span> (property &amp;&amp; property.configurable === <span class="literal">false</span>) <span class="keyword">return</span></span><br><span class="line">  <span class="built_in">Object</span>.defineProperty(target, key, &#123;</span><br><span class="line">    <span class="function"><span class="title">get</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">      <span class="keyword">return</span> value</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="function"><span class="title">set</span>(<span class="params">newValue</span>)</span> &#123;</span><br><span class="line">      <span class="keyword">if</span> (newValue !== value) &#123;</span><br><span class="line">        observer(newValue) <span class="comment">// 值修改后进行监听</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// value 一直在闭包中，此处设置完之后，再 get 时也是会获取最新的值</span></span><br><span class="line">        value = newValue</span><br><span class="line">        updateView() <span class="comment">// 触发更新视图</span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 监测数据的变化</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">observer</span>(<span class="params">target</span>) </span>&#123;</span><br><span class="line">  <span class="comment">// 不是对象或数组（vue 是判断是否是数组、纯粹对象、可扩展对象）</span></span><br><span class="line">  <span class="keyword">if</span> (<span class="keyword">typeof</span> target !== <span class="string">&#x27;object&#x27;</span> || target === <span class="literal">null</span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> target</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">// 不要这样写，污染全局的 Array 原型</span></span><br><span class="line">  <span class="comment">/* Array.prototype.push = function () &#123;</span></span><br><span class="line"><span class="comment">      updateView()</span></span><br><span class="line"><span class="comment">  &#125; */</span></span><br><span class="line">  <span class="keyword">if</span> (<span class="built_in">Array</span>.isArray(target)) &#123;</span><br><span class="line">    target.__proto__ = arrayMethods</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">// 重新定义各个属性（for in 也可以遍历数组）</span></span><br><span class="line">  <span class="keyword">for</span> (<span class="keyword">let</span> key <span class="keyword">in</span> target) &#123;</span><br><span class="line">    <span class="keyword">if</span> (!<span class="built_in">Object</span>.hasOwnProperty.call(target, key)) <span class="keyword">return</span></span><br><span class="line">    defineReactive(target, key, target[key])</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> data = &#123;</span><br><span class="line">  name: <span class="string">&#x27;zhangsan&#x27;</span>,</span><br><span class="line">  age: <span class="number">20</span>,</span><br><span class="line">  info: &#123;</span><br><span class="line">    address: <span class="string">&#x27;北京&#x27;</span>, <span class="comment">// 需要深度监听</span></span><br><span class="line">  &#125;,</span><br><span class="line">  nums: [<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>],</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">observer(data)</span><br><span class="line"></span><br><span class="line">data.name = <span class="string">&#x27;lisi&#x27;</span></span><br><span class="line">data.age = <span class="number">21</span></span><br><span class="line">data.info.address = <span class="string">&#x27;上海&#x27;</span> <span class="comment">// 深度监听</span></span><br><span class="line"><span class="comment">// data.x = &#x27;100&#x27; // 新增属性，监听不到 —— 所以有 Vue.set</span></span><br><span class="line"><span class="comment">// delete data.name // 删除属性，监听不到 —— 所有已 Vue.delete</span></span><br><span class="line">data.nums.push(<span class="number">4</span>) <span class="comment">// 监听数组</span></span><br></pre></td></tr></table></figure>

<h2 id="虚拟-DOM"><a href="#虚拟-DOM" class="headerlink" title="虚拟 DOM"></a>虚拟 DOM</h2><ul>
<li>DOM 操作非常耗费性能</li>
<li>以前用 jQuery，可以自行控制 DOM 操作的时机，手动调整</li>
<li>Vue 和 React 是数据驱动视图，如何有效控制 DOM 操作？</li>
</ul>
<h3 id="vdom"><a href="#vdom" class="headerlink" title="vdom"></a>vdom</h3><ul>
<li>有了一定复杂度，想减少计算次数比较难</li>
<li>能不能把计算，更多的转移为 JS 计算？因为 JS 执行速度很快</li>
<li>vdom——用 JS 模拟 DOM 结构，计算出最小的变更，操作 DOM</li>
</ul>
<p><strong>用 JS 模拟 DOM 结构</strong></p>
<ul>
<li>DOM 结构</li>
</ul>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;div1&quot;</span> <span class="attr">class</span>=<span class="string">&quot;container&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">p</span>&gt;</span>vdom<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">ul</span> <span class="attr">style</span>=<span class="string">&quot;font-size: 20px;&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>a<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>

<ul>
<li>JS 模拟</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  tag: <span class="string">&#x27;div&#x27;</span>,</span><br><span class="line">  props: &#123;</span><br><span class="line">    className: <span class="string">&#x27;container&#x27;</span>,</span><br><span class="line">    id: <span class="string">&#x27;div1&#x27;</span>,</span><br><span class="line">  &#125;,</span><br><span class="line">  children: [</span><br><span class="line">    &#123;</span><br><span class="line">      tag: <span class="string">&#x27;p&#x27;</span>,</span><br><span class="line">      children: <span class="string">&#x27;dom&#x27;</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      tag: <span class="string">&#x27;ul&#x27;</span>,</span><br><span class="line">      props: &#123; <span class="attr">style</span>: <span class="string">&#x27;font-size: 20px&#x27;</span> &#125;,</span><br><span class="line">      children: [&#123; <span class="attr">tag</span>: <span class="string">&#x27;li&#x27;</span>, <span class="attr">children</span>: <span class="string">&#x27;a&#x27;</span> &#125;],</span><br><span class="line">    &#125;,</span><br><span class="line">  ],</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>Vue 中的真实 DOM</li>
</ul>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;app&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">p</span> <span class="attr">class</span>=<span class="string">&quot;text&quot;</span>&gt;</span>HelloWorld<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>

<ul>
<li>Vue 中的虚拟 DOM</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;tag&quot;</span>: <span class="string">&quot;div&quot;</span>,                     <span class="comment">// 标签名</span></span><br><span class="line">  <span class="string">&quot;key&quot;</span>: <span class="literal">undefined</span>,                 <span class="comment">// key值</span></span><br><span class="line">  <span class="string">&quot;elm&quot;</span>: div#app,                   <span class="comment">// 真实DOM节点</span></span><br><span class="line">  <span class="string">&quot;text&quot;</span>: <span class="literal">undefined</span>,                <span class="comment">// 文本信息</span></span><br><span class="line">  <span class="string">&quot;data&quot;</span>: &#123;<span class="attr">attrs</span>: &#123;<span class="attr">id</span>:<span class="string">&quot;app&quot;</span>&#125;&#125;,      <span class="comment">// 节点属性</span></span><br><span class="line">  <span class="string">&quot;children&quot;</span>: [&#123;    	            <span class="comment">// 子节点属性</span></span><br><span class="line">    <span class="string">&quot;tag&quot;</span>: <span class="string">&quot;p&quot;</span>,</span><br><span class="line">    <span class="string">&quot;key&quot;</span>: <span class="literal">undefined</span>,</span><br><span class="line">    <span class="string">&quot;elm&quot;</span>: p.text,</span><br><span class="line">    <span class="string">&quot;text&quot;</span>: <span class="literal">undefined</span>,</span><br><span class="line">    <span class="string">&quot;data&quot;</span>: &#123;<span class="attr">attrs</span>: &#123;<span class="attr">class</span>: <span class="string">&quot;text&quot;</span>&#125;&#125;,</span><br><span class="line">    <span class="string">&quot;children&quot;</span>: [&#123;</span><br><span class="line">      <span class="string">&quot;tag&quot;</span>: <span class="literal">undefined</span>,</span><br><span class="line">      <span class="string">&quot;key&quot;</span>: <span class="literal">undefined</span>,</span><br><span class="line">      <span class="string">&quot;elm&quot;</span>: text,</span><br><span class="line">      <span class="string">&quot;text&quot;</span>: <span class="string">&quot;helloWorld&quot;</span>,</span><br><span class="line">      <span class="string">&quot;data&quot;</span>: <span class="literal">undefined</span>,</span><br><span class="line">      <span class="string">&quot;children&quot;</span>: []</span><br><span class="line">    &#125;]</span><br><span class="line">  &#125;]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="snabbdom-使用"><a href="#snabbdom-使用" class="headerlink" title="snabbdom 使用"></a>snabbdom 使用</h3><ul>
<li><p>通过 <strong><a target="_blank" rel="noopener" href="https://github.com/snabbdom/snabbdom">snabbdom</a></strong> 学习 vdom</p>
</li>
<li><p>核心概念：h、vnode、patch、diff、key 等</p>
</li>
<li><p>vdom 价值：数据驱动视图，控制 DOM 操作</p>
<p><code>patch(elem, vnode)</code> 和 <code>patch(vnode, newVnode)</code></p>
</li>
</ul>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;container&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;btn-change&quot;</span>&gt;</span>change<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;https://cdn.bootcss.com/snabbdom/0.7.3/snabbdom.js&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;https://cdn.bootcss.com/snabbdom/0.7.3/snabbdom-class.js&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;https://cdn.bootcss.com/snabbdom/0.7.3/snabbdom-props.js&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;https://cdn.bootcss.com/snabbdom/0.7.3/snabbdom-style.js&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;https://cdn.bootcss.com/snabbdom/0.7.3/snabbdom-eventlisteners.js&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;https://cdn.bootcss.com/snabbdom/0.7.3/h.js&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript">  <span class="keyword">const</span> snabbdom = <span class="built_in">window</span>.snabbdom</span></span><br><span class="line"></span><br><span class="line"><span class="javascript">  <span class="comment">// 定义 patch</span></span></span><br><span class="line"><span class="javascript">  <span class="keyword">const</span> patch = snabbdom.init([</span></span><br><span class="line">    snabbdom_class,</span><br><span class="line">    snabbdom_props,</span><br><span class="line">    snabbdom_style,</span><br><span class="line">    snabbdom_eventlisteners,</span><br><span class="line">  ])</span><br><span class="line"></span><br><span class="line"><span class="javascript">  <span class="comment">// 定义 h</span></span></span><br><span class="line"><span class="javascript">  <span class="keyword">const</span> h = snabbdom.h</span></span><br><span class="line"></span><br><span class="line"><span class="javascript">  <span class="keyword">const</span> container = <span class="built_in">document</span>.getElementById(<span class="string">&#x27;container&#x27;</span>)</span></span><br><span class="line"></span><br><span class="line"><span class="javascript">  <span class="comment">// 生成 vnode</span></span></span><br><span class="line"><span class="javascript">  <span class="keyword">const</span> vnode = h(<span class="string">&#x27;ul#list&#x27;</span>, &#123;&#125;, [h(<span class="string">&#x27;li.item&#x27;</span>, &#123;&#125;, <span class="string">&#x27;Item 1&#x27;</span>), h(<span class="string">&#x27;li.item&#x27;</span>, &#123;&#125;, <span class="string">&#x27;Item 2&#x27;</span>)])</span></span><br><span class="line">  patch(container, vnode)</span><br><span class="line"></span><br><span class="line"><span class="javascript">  <span class="built_in">document</span>.getElementById(<span class="string">&#x27;btn-change&#x27;</span>).addEventListener(<span class="string">&#x27;click&#x27;</span>, <span class="function">() =&gt;</span> &#123;</span></span><br><span class="line"><span class="javascript">    <span class="comment">// 生成 newVnode</span></span></span><br><span class="line"><span class="javascript">    <span class="keyword">const</span> newVnode = h(<span class="string">&#x27;ul#list&#x27;</span>, &#123;&#125;, [</span></span><br><span class="line"><span class="javascript">      h(<span class="string">&#x27;li.item&#x27;</span>, &#123;&#125;, <span class="string">&#x27;Item 1&#x27;</span>),</span></span><br><span class="line"><span class="javascript">      h(<span class="string">&#x27;li.item&#x27;</span>, &#123;&#125;, <span class="string">&#x27;Item B&#x27;</span>),</span></span><br><span class="line"><span class="javascript">      h(<span class="string">&#x27;li.item&#x27;</span>, &#123;&#125;, <span class="string">&#x27;Item 3&#x27;</span>),</span></span><br><span class="line">    ])</span><br><span class="line">    patch(vnode, newVnode)</span><br><span class="line">  &#125;)</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<ul>
<li>jQuery 版本</li>
</ul>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;container&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;btn-change&quot;</span>&gt;</span>change<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">&quot;text/javascript&quot;</span> <span class="attr">src</span>=<span class="string">&quot;https://cdn.bootcss.com/jquery/3.2.0/jquery.js&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">&quot;text/javascript&quot;</span>&gt;</span></span><br><span class="line"><span class="javascript">  <span class="keyword">const</span> data = [</span></span><br><span class="line">    &#123;</span><br><span class="line"><span class="javascript">      name: <span class="string">&#x27;张三&#x27;</span>,</span></span><br><span class="line"><span class="javascript">      age: <span class="string">&#x27;20&#x27;</span>,</span></span><br><span class="line"><span class="javascript">      address: <span class="string">&#x27;北京&#x27;</span>,</span></span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line"><span class="javascript">      name: <span class="string">&#x27;李四&#x27;</span>,</span></span><br><span class="line"><span class="javascript">      age: <span class="string">&#x27;21&#x27;</span>,</span></span><br><span class="line"><span class="javascript">      address: <span class="string">&#x27;上海&#x27;</span>,</span></span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line"><span class="javascript">      name: <span class="string">&#x27;王五&#x27;</span>,</span></span><br><span class="line"><span class="javascript">      age: <span class="string">&#x27;22&#x27;</span>,</span></span><br><span class="line"><span class="javascript">      address: <span class="string">&#x27;广州&#x27;</span>,</span></span><br><span class="line">    &#125;,</span><br><span class="line">  ]</span><br><span class="line"></span><br><span class="line"><span class="javascript">  <span class="comment">// 渲染函数</span></span></span><br><span class="line"><span class="javascript">  <span class="function"><span class="keyword">function</span> <span class="title">render</span>(<span class="params">data</span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">    <span class="keyword">const</span> $container = $(<span class="string">&#x27;#container&#x27;</span>)</span></span><br><span class="line"></span><br><span class="line"><span class="javascript">    <span class="comment">// 清空容器，重要！！！</span></span></span><br><span class="line"><span class="javascript">    $container.html(<span class="string">&#x27;&#x27;</span>)</span></span><br><span class="line"></span><br><span class="line"><span class="javascript">    <span class="comment">// 拼接 table</span></span></span><br><span class="line"><span class="javascript">    <span class="keyword">const</span> $table = $(<span class="string">&#x27;&lt;table&gt;&#x27;</span>)</span></span><br><span class="line"></span><br><span class="line"><span class="handlebars"><span class="xml">    $table.append($(&#x27;<span class="tag">&lt;<span class="name">tr</span>&gt;</span><span class="tag">&lt;<span class="name">td</span>&gt;</span>name<span class="tag">&lt;/<span class="name">td</span>&gt;</span><span class="tag">&lt;<span class="name">td</span>&gt;</span>age<span class="tag">&lt;/<span class="name">td</span>&gt;</span><span class="tag">&lt;<span class="name">td</span>&gt;</span>address<span class="tag">&lt;/<span class="name">td</span>&gt;</span>/tr&gt;&#x27;))</span></span></span><br><span class="line"><span class="javascript">    data.forEach(<span class="function"><span class="params">item</span> =&gt;</span> &#123;</span></span><br><span class="line">      $table.append(</span><br><span class="line"><span class="javascript">        $(</span></span><br><span class="line"><span class="handlebars"><span class="xml">          &#x27;<span class="tag">&lt;<span class="name">tr</span>&gt;</span><span class="tag">&lt;<span class="name">td</span>&gt;</span>&#x27; + item.name + &#x27;<span class="tag">&lt;/<span class="name">td</span>&gt;</span><span class="tag">&lt;<span class="name">td</span>&gt;</span>&#x27; + item.age + &#x27;<span class="tag">&lt;/<span class="name">td</span>&gt;</span><span class="tag">&lt;<span class="name">td</span>&gt;</span>&#x27; + item.address + &#x27;<span class="tag">&lt;/<span class="name">td</span>&gt;</span>/tr&gt;&#x27;</span></span></span><br><span class="line">        )</span><br><span class="line">      )</span><br><span class="line">    &#125;)</span><br><span class="line"></span><br><span class="line"><span class="javascript">    <span class="comment">// 渲染到页面</span></span></span><br><span class="line">    $container.append($table)</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line"><span class="javascript">  $(<span class="string">&#x27;#btn-change&#x27;</span>).click(<span class="function">() =&gt;</span> &#123;</span></span><br><span class="line">    data[1].age = 30</span><br><span class="line"><span class="javascript">    data[<span class="number">2</span>].address = <span class="string">&#x27;深圳&#x27;</span></span></span><br><span class="line"><span class="javascript">    <span class="comment">// re-render  再次渲染</span></span></span><br><span class="line">    render(data)</span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line"><span class="javascript">  <span class="comment">// 页面加载完立刻执行（初次渲染）</span></span></span><br><span class="line">  render(data)</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<ul>
<li>vdom 版本</li>
</ul>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;container&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;btn-change&quot;</span>&gt;</span>change<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;https://cdn.bootcss.com/snabbdom/0.7.3/snabbdom.js&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;https://cdn.bootcss.com/snabbdom/0.7.3/snabbdom-class.js&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;https://cdn.bootcss.com/snabbdom/0.7.3/snabbdom-props.js&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;https://cdn.bootcss.com/snabbdom/0.7.3/snabbdom-style.js&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;https://cdn.bootcss.com/snabbdom/0.7.3/snabbdom-eventlisteners.js&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;https://cdn.bootcss.com/snabbdom/0.7.3/h.js&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">&quot;text/javascript&quot;</span>&gt;</span></span><br><span class="line"><span class="javascript">  <span class="keyword">const</span> snabbdom = <span class="built_in">window</span>.snabbdom</span></span><br><span class="line"><span class="javascript">  <span class="comment">// 定义关键函数 patch</span></span></span><br><span class="line"><span class="javascript">  <span class="keyword">const</span> patch = snabbdom.init([</span></span><br><span class="line">    snabbdom_class,</span><br><span class="line">    snabbdom_props,</span><br><span class="line">    snabbdom_style,</span><br><span class="line">    snabbdom_eventlisteners,</span><br><span class="line">  ])</span><br><span class="line"></span><br><span class="line"><span class="javascript">  <span class="comment">// 定义关键函数 h</span></span></span><br><span class="line"><span class="javascript">  <span class="keyword">const</span> h = snabbdom.h</span></span><br><span class="line"></span><br><span class="line"><span class="javascript">  <span class="comment">// 原始数据</span></span></span><br><span class="line"><span class="javascript">  <span class="keyword">const</span> data = [</span></span><br><span class="line">    &#123;</span><br><span class="line"><span class="javascript">      name: <span class="string">&#x27;张三&#x27;</span>,</span></span><br><span class="line"><span class="javascript">      age: <span class="string">&#x27;20&#x27;</span>,</span></span><br><span class="line"><span class="javascript">      address: <span class="string">&#x27;北京&#x27;</span>,</span></span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line"><span class="javascript">      name: <span class="string">&#x27;李四&#x27;</span>,</span></span><br><span class="line"><span class="javascript">      age: <span class="string">&#x27;21&#x27;</span>,</span></span><br><span class="line"><span class="javascript">      address: <span class="string">&#x27;上海&#x27;</span>,</span></span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line"><span class="javascript">      name: <span class="string">&#x27;王五&#x27;</span>,</span></span><br><span class="line"><span class="javascript">      age: <span class="string">&#x27;22&#x27;</span>,</span></span><br><span class="line"><span class="javascript">      address: <span class="string">&#x27;广州&#x27;</span>,</span></span><br><span class="line">    &#125;,</span><br><span class="line">  ]</span><br><span class="line"><span class="javascript">  <span class="comment">// 把表头也放在 data 中</span></span></span><br><span class="line">  data.unshift(&#123;</span><br><span class="line"><span class="javascript">    name: <span class="string">&#x27;姓名&#x27;</span>,</span></span><br><span class="line"><span class="javascript">    age: <span class="string">&#x27;年龄&#x27;</span>,</span></span><br><span class="line"><span class="javascript">    address: <span class="string">&#x27;地址&#x27;</span>,</span></span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line"><span class="javascript">  <span class="keyword">const</span> container = <span class="built_in">document</span>.getElementById(<span class="string">&#x27;container&#x27;</span>)</span></span><br><span class="line"></span><br><span class="line"><span class="javascript">  <span class="comment">// 渲染函数</span></span></span><br><span class="line"><span class="javascript">  <span class="keyword">let</span> vnode</span></span><br><span class="line"><span class="javascript">  <span class="function"><span class="keyword">function</span> <span class="title">render</span>(<span class="params">data</span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">    <span class="keyword">const</span> newVnode = h(</span></span><br><span class="line"><span class="javascript">      <span class="string">&#x27;table&#x27;</span>,</span></span><br><span class="line">      &#123;&#125;,</span><br><span class="line"><span class="javascript">      data.map(<span class="function"><span class="params">item</span> =&gt;</span> &#123;</span></span><br><span class="line"><span class="javascript">        <span class="keyword">const</span> tds = []</span></span><br><span class="line"><span class="javascript">        <span class="keyword">for</span> (<span class="keyword">let</span> i <span class="keyword">in</span> item) &#123;</span></span><br><span class="line"><span class="javascript">          <span class="keyword">if</span> (item.hasOwnProperty(i)) &#123;</span></span><br><span class="line"><span class="javascript">            tds.push(h(<span class="string">&#x27;td&#x27;</span>, &#123;&#125;, item[i] + <span class="string">&#x27;&#x27;</span>))</span></span><br><span class="line">          &#125;</span><br><span class="line">        &#125;</span><br><span class="line"><span class="javascript">        <span class="keyword">return</span> h(<span class="string">&#x27;tr&#x27;</span>, &#123;&#125;, tds)</span></span><br><span class="line">      &#125;)</span><br><span class="line">    )</span><br><span class="line"></span><br><span class="line"><span class="javascript">    <span class="keyword">if</span> (vnode) &#123;</span></span><br><span class="line"><span class="javascript">      <span class="comment">// re-render</span></span></span><br><span class="line">      patch(vnode, newVnode)</span><br><span class="line"><span class="javascript">    &#125; <span class="keyword">else</span> &#123;</span></span><br><span class="line"><span class="javascript">      <span class="comment">// 初次渲染</span></span></span><br><span class="line">      patch(container, newVnode)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="javascript">    <span class="comment">// 存储当前的 vnode 结果</span></span></span><br><span class="line">    vnode = newVnode</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line"><span class="javascript">  <span class="comment">// 初次渲染</span></span></span><br><span class="line">  render(data)</span><br><span class="line"></span><br><span class="line"><span class="javascript">  <span class="keyword">const</span> btnChange = <span class="built_in">document</span>.getElementById(<span class="string">&#x27;btn-change&#x27;</span>)</span></span><br><span class="line"><span class="javascript">  btnChange.addEventListener(<span class="string">&#x27;click&#x27;</span>, <span class="function">() =&gt;</span> &#123;</span></span><br><span class="line">    data[1].age = 30</span><br><span class="line"><span class="javascript">    data[<span class="number">2</span>].address = <span class="string">&#x27;深圳&#x27;</span></span></span><br><span class="line"><span class="javascript">    <span class="comment">// re-render</span></span></span><br><span class="line">    render(data)</span><br><span class="line">  &#125;)</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h2 id="diff-算法"><a href="#diff-算法" class="headerlink" title="diff 算法"></a>diff 算法</h2><blockquote>
<p>推荐文章：<a target="_blank" rel="noopener" href="https://juejin.cn/post/6971622260490797069#refetch">图文并茂地来详细讲讲Vue Diff算法</a></p>
</blockquote>
<ul>
<li>diff 算法是 vdom 中最核心、最关键的部分</li>
<li>diff 算法能在日常使用 Vue、React 中体现出来（如：key）</li>
</ul>
<p><strong>diff 算法概述：</strong></p>
<ul>
<li>diff 即对比。是一个广泛的概念，如 linux diff 命令、git diff 等</li>
<li>两个 js 对象也可以做 diff，**<a target="_blank" rel="noopener" href="https://github.com/cujojs/jiff">jiff</a>**</li>
<li>两棵树做 diff，如这里的 vdom diff</li>
</ul>
<p><img data-src="https://gitee.com/lilyn/pic/raw/master/jslearn-img/diff%E7%AE%97%E6%B3%95%E6%A6%82%E8%BF%B0.png"></p>
<p><strong>树 diff 的时间复杂度 O(n^3)</strong></p>
<ul>
<li><p>对于旧树上的点 E 来说，它要和新树上的所有点比较，复杂度为 O(n)</p>
</li>
<li><p>点 E 在新树上没有找到，点 E 会被删除，然后遍历新树上的所有点找到对应点（X）去填空，复杂度增加到 O(n^2)</p>
</li>
<li><p>这样的操作会在旧树的每个点进行，最终复杂度为 O(n^3)</p>
<p>1000 个节点，要计算 1 亿次，算法不可用</p>
</li>
</ul>
<p><strong>优化时间复杂度到 O(n)</strong></p>
<ul>
<li>只比较同一层级，不跨级比较</li>
<li>tag 不相同，则直接删掉重建，不再深度比较</li>
<li>tag 和 key，两者都相同，则认为是相同节点，不再深度比较</li>
</ul>
<p><img data-src="https://gitee.com/lilyn/pic/raw/master/jslearn-img/diff%E7%AE%97%E6%B3%95%E6%97%B6%E9%97%B4%E5%A4%8D%E6%9D%82%E5%BA%A6.png"></p>
<p><img data-src="https://gitee.com/lilyn/pic/raw/master/jslearn-img/diff%E7%AE%97%E6%B3%95%E6%97%B6%E9%97%B4%E5%A4%8D%E6%9D%82%E5%BA%A62.png"></p>
<h3 id="snabbdom-源码"><a href="#snabbdom-源码" class="headerlink" title="snabbdom 源码"></a>snabbdom 源码</h3><blockquote>
<p><a target="_blank" rel="noopener" href="https://github.com/snabbdom/snabbdom">snabbdom</a></p>
</blockquote>
<ul>
<li><p><code>src\h.ts</code></p>
<p>最后返回 vnode</p>
</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">h</span>(<span class="params">sel: string</span>): <span class="title">VNode</span></span>;</span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">h</span>(<span class="params">sel: string, data: VNodeData | <span class="literal">null</span></span>): <span class="title">VNode</span></span>;</span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">h</span>(<span class="params">sel: string, children: VNodeChildren</span>): <span class="title">VNode</span></span>;</span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">h</span>(<span class="params"></span></span></span><br><span class="line"><span class="function"><span class="params">  sel: string,</span></span></span><br><span class="line"><span class="function"><span class="params">  data: VNodeData | <span class="literal">null</span>,</span></span></span><br><span class="line"><span class="function"><span class="params">  children: VNodeChildren</span></span></span><br><span class="line"><span class="function"><span class="params"></span>): <span class="title">VNode</span></span>;</span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">h</span>(<span class="params">sel: any, b?: any, c?: any</span>): <span class="title">VNode</span> </span>&#123;</span><br><span class="line">  <span class="comment">// ...</span></span><br><span class="line">  <span class="keyword">return</span> vnode(sel, data, children, text, <span class="literal">undefined</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li><p><code>src\vnode.ts</code></p>
<p>最后返回一个 JS 对象</p>
</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">vnode</span>(<span class="params"></span></span></span><br><span class="line"><span class="function"><span class="params">  sel: string | <span class="literal">undefined</span>,</span></span></span><br><span class="line"><span class="function"><span class="params">  data: any | <span class="literal">undefined</span>,</span></span></span><br><span class="line"><span class="function"><span class="params">  children: <span class="built_in">Array</span>&lt;VNode | string&gt; | <span class="literal">undefined</span>,</span></span></span><br><span class="line"><span class="function"><span class="params">  text: string | <span class="literal">undefined</span>,</span></span></span><br><span class="line"><span class="function"><span class="params">  elm: Element | DocumentFragment | Text | <span class="literal">undefined</span></span></span></span><br><span class="line"><span class="function"><span class="params"></span>): <span class="title">VNode</span> </span>&#123;</span><br><span class="line">  <span class="keyword">const</span> key = data === <span class="literal">undefined</span> ? <span class="literal">undefined</span> : data.key;</span><br><span class="line">  <span class="keyword">return</span> &#123; sel, data, children, text, elm, key &#125;;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li><p><code>src\init.ts</code></p>
<p>触发页面更新就会调用 <code>patch</code> 方法，之后我们看一下 <code>patch</code> 的源码</p>
</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">return</span> <span class="function"><span class="keyword">function</span> <span class="title">patch</span>(<span class="params"></span></span></span><br><span class="line"><span class="function"><span class="params">  oldVnode: VNode | Element | DocumentFragment,</span></span></span><br><span class="line"><span class="function"><span class="params">  vnode: VNode</span></span></span><br><span class="line"><span class="function"><span class="params"></span>): <span class="title">VNode</span> </span>&#123;</span><br><span class="line">  <span class="keyword">let</span> i: number, <span class="attr">elm</span>: Node, <span class="attr">parent</span>: Node;</span><br><span class="line">  <span class="keyword">const</span> insertedVnodeQueue: VNodeQueue = [];</span><br><span class="line">  <span class="comment">// 执行 pre hooks</span></span><br><span class="line">  <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; cbs.pre.length; ++i) cbs.pre[i]();</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 第一个参数不是 vnode</span></span><br><span class="line">  <span class="keyword">if</span> (isElement(api, oldVnode)) &#123;</span><br><span class="line">    <span class="comment">// 创建一个空的 vnode，关联到这个 DOM 元素</span></span><br><span class="line">    oldVnode = emptyNodeAt(oldVnode);</span><br><span class="line">  &#125; <span class="keyword">else</span> <span class="keyword">if</span> (isDocumentFragment(api, oldVnode)) &#123;</span><br><span class="line">    oldVnode = emptyDocumentFragmentAt(oldVnode);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 相同的 vnode（key 和 sel 都相同）</span></span><br><span class="line">  <span class="keyword">if</span> (sameVnode(oldVnode, vnode)) &#123;</span><br><span class="line">    <span class="comment">// vnode 对比</span></span><br><span class="line">    patchVnode(oldVnode, vnode, insertedVnodeQueue);</span><br><span class="line">  <span class="comment">// 不同的 vnode，直接删掉重建</span></span><br><span class="line">  &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    elm = oldVnode.elm!;</span><br><span class="line">    parent = api.parentNode(elm) <span class="keyword">as</span> Node;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 重建</span></span><br><span class="line">    createElm(vnode, insertedVnodeQueue);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (parent !== <span class="literal">null</span>) &#123;</span><br><span class="line">      api.insertBefore(parent, vnode.elm!, api.nextSibling(elm));</span><br><span class="line">      removeVnodes(parent, [oldVnode], <span class="number">0</span>, <span class="number">0</span>);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; insertedVnodeQueue.length; ++i) &#123;</span><br><span class="line">    insertedVnodeQueue[i].data!.hook!.insert!(insertedVnodeQueue[i]);</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; cbs.post.length; ++i) cbs.post[i]();</span><br><span class="line">  <span class="keyword">return</span> vnode;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>如果 <code>patch</code> 第一个参数不是 vnode，创建一个空的 vnode</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">emptyNodeAt</span>(<span class="params">elm: Element</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">const</span> id = elm.id ? <span class="string">&quot;#&quot;</span> + elm.id : <span class="string">&quot;&quot;</span>;</span><br><span class="line">  <span class="keyword">const</span> classes = elm.getAttribute(<span class="string">&quot;class&quot;</span>);</span><br><span class="line">  <span class="keyword">const</span> c = classes ? <span class="string">&quot;.&quot;</span> + classes.split(<span class="string">&quot; &quot;</span>).join(<span class="string">&quot;.&quot;</span>) : <span class="string">&quot;&quot;</span>;</span><br><span class="line">  <span class="keyword">return</span> vnode(</span><br><span class="line">    api.tagName(elm).toLowerCase() + id + c,</span><br><span class="line">    &#123;&#125;,</span><br><span class="line">    [],</span><br><span class="line">    <span class="literal">undefined</span>,</span><br><span class="line">    elm</span><br><span class="line">  );</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>比较 vnode 是否相同（比较 key 和 selector）</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">sameVnode</span>(<span class="params">vnode1: VNode, vnode2: VNode</span>): <span class="title">boolean</span> </span>&#123;</span><br><span class="line">  <span class="comment">// key 和 sel 或 data 都相等就是相等</span></span><br><span class="line">  <span class="keyword">const</span> isSameKey = vnode1.key === vnode2.key;</span><br><span class="line">  <span class="keyword">const</span> isSameIs = vnode1.data?.is === vnode2.data?.is;</span><br><span class="line">  <span class="keyword">const</span> isSameSel = vnode1.sel === vnode2.sel;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">return</span> isSameSel &amp;&amp; isSameKey &amp;&amp; isSameIs;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="patchVnode"><a href="#patchVnode" class="headerlink" title="patchVnode"></a>patchVnode</h3><p>vnode 相同的话执行 <code>patchVnode</code></p>
<ul>
<li>两者都有 children -&gt; 进行 children 对比，之后更新 children（updateChildren）</li>
<li>新 children 有，旧 children 无 -&gt; 清空旧 text，添加 children（addVnodes）</li>
<li>新 children 无，旧 children 有 -&gt; 移除旧 children（removeVnodes）</li>
<li>新 children 无，旧 children 无，旧 text 有  -&gt; 清空旧 text</li>
<li>新旧 text 不一样 -&gt; 移除旧 children(removeVnodes)，设置 text</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">patchVnode</span>(<span class="params"></span></span></span><br><span class="line"><span class="function"><span class="params">  oldVnode: VNode,</span></span></span><br><span class="line"><span class="function"><span class="params">  vnode: VNode,</span></span></span><br><span class="line"><span class="function"><span class="params">  insertedVnodeQueue: VNodeQueue</span></span></span><br><span class="line"><span class="function"><span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="comment">// 执行 prepatch hook，生命周期钩子</span></span><br><span class="line">  <span class="keyword">const</span> hook = vnode.data?.hook;</span><br><span class="line">  hook?.prepatch?.(oldVnode, vnode);</span><br><span class="line">  <span class="comment">// 设置 vnode.elm</span></span><br><span class="line">  <span class="keyword">const</span> elm = (vnode.elm = oldVnode.elm)!;</span><br><span class="line">  <span class="comment">// 旧的 children</span></span><br><span class="line">  <span class="keyword">const</span> oldCh = oldVnode.children <span class="keyword">as</span> VNode[];</span><br><span class="line">  <span class="comment">// 新的 children</span></span><br><span class="line">  <span class="keyword">const</span> ch = vnode.children <span class="keyword">as</span> VNode[];</span><br><span class="line">  <span class="keyword">if</span> (oldVnode === vnode) <span class="keyword">return</span>;</span><br><span class="line">  <span class="comment">// hook 相关</span></span><br><span class="line">  <span class="keyword">if</span> (vnode.data !== <span class="literal">undefined</span>) &#123;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; cbs.update.length; ++i)</span><br><span class="line">      cbs.update[i](oldVnode, vnode);</span><br><span class="line">    vnode.data.hook?.update?.(oldVnode, vnode);</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">// 新的 vnode.text===undefined (vnode.children 一般有值)</span></span><br><span class="line">  <span class="keyword">if</span> (isUndef(vnode.text)) &#123;</span><br><span class="line">    <span class="comment">// 新旧都有 children</span></span><br><span class="line">    <span class="keyword">if</span> (isDef(oldCh) &amp;&amp; isDef(ch)) &#123;</span><br><span class="line">      <span class="keyword">if</span> (oldCh !== ch) updateChildren(elm, oldCh, ch, insertedVnodeQueue);</span><br><span class="line">    <span class="comment">// 新 children 有，旧 children 无（旧 text 有）</span></span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (isDef(ch)) &#123;</span><br><span class="line">      <span class="comment">// 清空 text</span></span><br><span class="line">      <span class="keyword">if</span> (isDef(oldVnode.text)) api.setTextContent(elm, <span class="string">&quot;&quot;</span>);</span><br><span class="line">      <span class="comment">// 添加 children</span></span><br><span class="line">      addVnodes(elm, <span class="literal">null</span>, ch, <span class="number">0</span>, ch.length - <span class="number">1</span>, insertedVnodeQueue);</span><br><span class="line">    <span class="comment">// 旧 children 有，新 children 无</span></span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (isDef(oldCh)) &#123;</span><br><span class="line">      <span class="comment">// 移除 children</span></span><br><span class="line">      removeVnodes(elm, oldCh, <span class="number">0</span>, oldCh.length - <span class="number">1</span>);</span><br><span class="line">    <span class="comment">// 旧 text 有</span></span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (isDef(oldVnode.text)) &#123;</span><br><span class="line">      api.setTextContent(elm, <span class="string">&quot;&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">  <span class="comment">// vnode.text!==undefined（vnode.children 无值）</span></span><br><span class="line">  &#125; <span class="keyword">else</span> <span class="keyword">if</span> (oldVnode.text !== vnode.text) &#123;</span><br><span class="line">    <span class="comment">// 移除旧 children</span></span><br><span class="line">    <span class="keyword">if</span> (isDef(oldCh)) &#123;</span><br><span class="line">      removeVnodes(elm, oldCh, <span class="number">0</span>, oldCh.length - <span class="number">1</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    api.setTextContent(elm, vnode.text!);</span><br><span class="line">  &#125;</span><br><span class="line">  hook?.postpatch?.(oldVnode, vnode);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="addVnodes-removeVnodes"><a href="#addVnodes-removeVnodes" class="headerlink" title="addVnodes removeVnodes"></a>addVnodes removeVnodes</h3><ul>
<li>新增 vnode</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">addVnodes</span>(<span class="params"></span></span></span><br><span class="line"><span class="function"><span class="params">  parentElm: Node,</span></span></span><br><span class="line"><span class="function"><span class="params">  before: Node | <span class="literal">null</span>,</span></span></span><br><span class="line"><span class="function"><span class="params">  vnodes: VNode[],</span></span></span><br><span class="line"><span class="function"><span class="params">  startIdx: number,</span></span></span><br><span class="line"><span class="function"><span class="params">  endIdx: number,</span></span></span><br><span class="line"><span class="function"><span class="params">  insertedVnodeQueue: VNodeQueue</span></span></span><br><span class="line"><span class="function"><span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">for</span> (; startIdx &lt;= endIdx; ++startIdx) &#123;</span><br><span class="line">    <span class="keyword">const</span> ch = vnodes[startIdx];</span><br><span class="line">    <span class="keyword">if</span> (ch != <span class="literal">null</span>) &#123;</span><br><span class="line">      api.insertBefore(parentElm, createElm(ch, insertedVnodeQueue), before);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>删除 vnode</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">removeVnodes</span>(<span class="params"></span></span></span><br><span class="line"><span class="function"><span class="params">  parentElm: Node,</span></span></span><br><span class="line"><span class="function"><span class="params">  vnodes: VNode[],</span></span></span><br><span class="line"><span class="function"><span class="params">  startIdx: number,</span></span></span><br><span class="line"><span class="function"><span class="params">  endIdx: number</span></span></span><br><span class="line"><span class="function"><span class="params"></span>): <span class="title">void</span> </span>&#123;</span><br><span class="line">  <span class="keyword">for</span> (; startIdx &lt;= endIdx; ++startIdx) &#123;</span><br><span class="line">    <span class="keyword">let</span> listeners: number;</span><br><span class="line">    <span class="keyword">let</span> rm: <span class="function">() =&gt;</span> <span class="keyword">void</span>;</span><br><span class="line">    <span class="keyword">const</span> ch = vnodes[startIdx];</span><br><span class="line">    <span class="keyword">if</span> (ch != <span class="literal">null</span>) &#123;</span><br><span class="line">      <span class="keyword">if</span> (isDef(ch.sel)) &#123;</span><br><span class="line">        invokeDestroyHook(ch); <span class="comment">// hook操作</span></span><br><span class="line">        <span class="comment">// 移除DOM元素</span></span><br><span class="line">        listeners = cbs.remove.length + <span class="number">1</span>;</span><br><span class="line">        rm = createRmCb(ch.elm!, listeners);</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; cbs.remove.length; ++i) cbs.remove[i](ch, rm);</span><br><span class="line">        <span class="keyword">const</span> removeHook = ch?.data?.hook?.remove;</span><br><span class="line">        <span class="keyword">if</span> (isDef(removeHook)) &#123;</span><br><span class="line">          removeHook(ch, rm);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">          rm();</span><br><span class="line">        &#125;</span><br><span class="line">      &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="comment">// Text node</span></span><br><span class="line">        api.removeChild(parentElm, ch.elm!);</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="updateChildren"><a href="#updateChildren" class="headerlink" title="updateChildren"></a>updateChildren</h3><p>新旧都有 children，会对 children 进行 updateChildren</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">updateChildren</span>(<span class="params"></span></span></span><br><span class="line"><span class="function"><span class="params">  parentElm: Node,</span></span></span><br><span class="line"><span class="function"><span class="params">  oldCh: VNode[],</span></span></span><br><span class="line"><span class="function"><span class="params">  newCh: VNode[],</span></span></span><br><span class="line"><span class="function"><span class="params">  insertedVnodeQueue: VNodeQueue</span></span></span><br><span class="line"><span class="function"><span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">while</span> (oldStartIdx &lt;= oldEndIdx &amp;&amp; newStartIdx &lt;= newEndIdx) &#123;</span><br><span class="line">    <span class="keyword">if</span> (oldStartVnode == <span class="literal">null</span>) &#123; <span class="comment">/* ... */</span> &#125;</span><br><span class="line">    <span class="comment">// 开始和开始对比</span></span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (sameVnode(oldStartVnode, newStartVnode)) &#123;</span><br><span class="line">    <span class="comment">// 结束和结束对比</span></span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (sameVnode(oldEndVnode, newEndVnode)) &#123;</span><br><span class="line">    <span class="comment">// 开始和结束对比</span></span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (sameVnode(oldStartVnode, newEndVnode)) &#123;</span><br><span class="line">    <span class="comment">// 结束和开始对比</span></span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (sameVnode(oldEndVnode, newStartVnode)) &#123;</span><br><span class="line">    <span class="comment">// 以上4个都未命中</span></span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      <span class="comment">// 拿新节点的 key，能否对应上 oldCh 中的某个节点的 key</span></span><br><span class="line">      idxInOld = oldKeyToIdx[newStartVnode.key <span class="keyword">as</span> string];</span><br><span class="line">      <span class="comment">// 没对应上</span></span><br><span class="line">      <span class="keyword">if</span> (isUndef(idxInOld)) &#123;</span><br><span class="line">        <span class="comment">// New element</span></span><br><span class="line">        api.insertBefore(</span><br><span class="line">          parentElm,</span><br><span class="line">          createElm(newStartVnode, insertedVnodeQueue),</span><br><span class="line">          oldStartVnode.elm!</span><br><span class="line">        );</span><br><span class="line">      &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="comment">// 对应上 key 的节点</span></span><br><span class="line">        elmToMove = oldCh[idxInOld];</span><br><span class="line">        <span class="comment">// sel 是否相同（sameVnode 的条件）</span></span><br><span class="line">        <span class="keyword">if</span> (elmToMove.sel !== newStartVnode.sel) &#123;</span><br><span class="line">          <span class="comment">// New element</span></span><br><span class="line">          api.insertBefore(</span><br><span class="line">            parentElm,</span><br><span class="line">            createElm(newStartVnode, insertedVnodeQueue),</span><br><span class="line">            oldStartVnode.elm!</span><br><span class="line">          );</span><br><span class="line">        <span class="comment">// sel 相等，key 相等</span></span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">          patchVnode(elmToMove, newStartVnode, insertedVnodeQueue);</span><br><span class="line">          oldCh[idxInOld] = <span class="literal">undefined</span> <span class="keyword">as</span> any;</span><br><span class="line">          api.insertBefore(parentElm, elmToMove.elm!, oldStartVnode.elm!);</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">      newStartVnode = newCh[++newStartIdx];</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><img data-src="https://gitee.com/lilyn/pic/raw/master/jslearn-img/updateChildren2.png"></p>
<p>不使用 key 就会全部删掉然后插入；使用 key ，key 相同会直接移动过来，不用做销毁然后重新渲染的过程</p>
<p><img data-src="https://gitee.com/lilyn/pic/raw/master/jslearn-img/updateChildren3.png"></p>
<h3 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h3><p>在 Vue 中，主要是 <code>patch()</code>、<code>patchVnode()</code> 和 <code>updateChildren</code> 这三个方法来实现 <code>Diff</code> 的</p>
<ul>
<li><p>当 Vue 中的响应式数据发生变化时，就会触发 <code>updateCompoent()</code></p>
</li>
<li><p><code>updateComponent()</code> 会调用 <code>patch()</code> 方法，在该方法中进行比较，调用 <code>sameVnode</code> 判断是否为相同节点（判断 <code>key</code>、<code>tag</code> 等静态属性），如果是相同节点的话执行 <code>patchVnode</code> 方法，开始比较节点差异，如果不是相同节点的话，则进行替换操作</p>
<p><code>patch()</code> 接收新旧虚拟 DOM，即 <code>oldVnode</code>、<code>vnode</code></p>
<ul>
<li>首先判断 <code>vnode</code> 是否存在，如果不存在，删除旧节点</li>
<li>如果 <code>vnode</code> 存在，再判断 <code>oldVnode</code>，如果不存在，只需新增整个 <code>vnode</code> 即可</li>
<li>如果 <code>vnode</code> 和 <code>oldVnode</code> 都存在，判断两者是不是相同节点，如果是，调用 <code>patchVnode()</code> ，对两个节点进行详细比较</li>
<li>如果两者不是相同节点，只需将 <code>vnode</code> 转换为真实 DOM 替换 <code>oldVnode</code></li>
</ul>
</li>
<li><p><code>patchVnode</code> 同样接收新旧虚拟 DOM，即 <code>oldVnode</code>、<code>vnode</code></p>
<ul>
<li>首先判断两个虚拟 DOM 是不是全等，即没有任何变动，是的话直接结束函数，否者继续执行</li>
<li>其次更新节点的属性，接着判断 <code>vnode.text</code> 是否存在，存在的话只需更新节点文本即可，否则继续执行</li>
<li>判断 <code>vnode</code> 和 <code>oldVnode</code> 是否有孩子节点<ul>
<li>如果两者都有孩子节点，执行 <code>updateChildren()</code> ，进行比较更新</li>
<li>如果 <code>vnode</code> 有孩子，<code>oldVnode</code> 没有，则直接删除所有孩子节点，并将该文本属性设为空</li>
<li>如果 <code>oldVnode</code> 有孩子，<code>vnode</code> 没有，则直接删除所有孩子节点</li>
<li>如果两者都没有孩子节点，就判断 <code>oldVnode.text</code> 是否有内容，有的话情况内容即可</li>
</ul>
</li>
</ul>
</li>
<li><p><code>updateChildren</code> 接收三个参数：<code>parentElm</code> 父级真实节点、<code>oldCh</code> 为 <code>oldVnode</code> 的孩子节点、<code>newCh</code> 为 <code>Vnode</code> 的孩子节点</p>
<p><code>oldCh</code> 和 <code>newCh</code> 都是一个数组。正常我们想到的方法就是对这两个数组一一比较，时间复杂度为 <code>O(NM)</code>。Vue 中是通过四个指针实现的</p>
<ul>
<li>首先是 <code>oldStartVnode</code> 和 <code>newStartVnode</code> 进行比较（两头比较），如果比较相同的话，就可以执行 <code>patchVnode</code></li>
<li>如果 <code>oldStartVnode</code> 和 <code>newStartVnode</code> 匹配不上的话，接下来就是 <code>oldEndVnode</code> 和 <code>newEndVnode</code> 做比较了（两尾比较）</li>
<li>如果两头和两尾比较都不是相同节点的话，就开始交叉比较，首先是 <code>oldStartVnode</code> 和 <code>newEndVnode</code> 做比较（头尾比较）</li>
<li>如果 <code>oldStartVnode</code> 和 <code>newEndVnode</code> 匹配不上的话，就 <code>oldEndVnode</code> 和 <code>newStartVnode</code> 进行比较（尾头比较）</li>
</ul>
<p>如果这四种比较方法都匹配不到相同节点，才是用暴力解法，针对 <code>newStartVnode</code> 去遍历 <code>oldCh</code> 中剩余的节点，一一匹配</p>
</li>
</ul>
<p>在 <code>sameVnode</code> 中，比较两个节点是否相同时，第一个判断条件就是 <code>vnode.key</code> ，并且在后面是用暴力解法时，第一选择也是通过 <code>key</code> 去匹配</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--  old  --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">p</span>&gt;</span>A<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">p</span>&gt;</span>B<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">p</span>&gt;</span>C<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!--  new  --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">p</span>&gt;</span>B<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">p</span>&gt;</span>C<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">p</span>&gt;</span>A<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>如果没有设置 <code>key</code> 值，通过 <code>diff</code> 需要操作 DOM 次数会很多，因为 <code>key</code> 为 <code>undefined</code>，即使每个标签都是相同节点，也会一一进行替换，需要操作 3 次 DOM</p>
<p>如果分别给对应添加了 <code>key</code> 值，通过 <code>diff</code> 只需操作 1 次 DOM</p>
<blockquote>
<p>图片来源：<a target="_blank" rel="noopener" href="https://juejin.cn/post/6971622260490797069#heading-2">图文并茂地来详细讲讲Vue Diff算法</a></p>
</blockquote>
<p><img data-src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/d84848aa035b4472b4d3f8e4b0d708c5~tplv-k3u1fbpfcp-watermark.awebp" alt="diff.jpg"></p>
<h2 id="模板编译"><a href="#模板编译" class="headerlink" title="模板编译"></a>模板编译</h2><ul>
<li>with 语法</li>
<li>模板到 render 函数，再到 vnode，再到渲染和更新</li>
<li>vue 组件可以用 render 代替 template</li>
</ul>
<p><strong>with 语法</strong></p>
<ul>
<li>使用 <code>with</code>，能改变 <code>&#123;&#125;</code> 内自由变量的查找规则</li>
<li>如果找不到匹配的 <code>obj</code> 属性，就会报错</li>
<li>with 要慎用，它打破了作用域规则，易读性变差</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> obj = &#123; <span class="attr">a</span>: <span class="number">100</span> &#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(obj.a)</span><br><span class="line"><span class="built_in">console</span>.log(obj.b) <span class="comment">// undefined</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">with</span> (obj) &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(a)</span><br><span class="line">  <span class="built_in">console</span>.log(b) <span class="comment">// 报错</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="vue-模板被编译成什么"><a href="#vue-模板被编译成什么" class="headerlink" title="vue 模板被编译成什么"></a>vue 模板被编译成什么</h3><ul>
<li>html 是标签语言，只有 JS 才能实现判断、循环，因此模板一定是转换为某种 JS 代码，即编译模板</li>
<li>使用 webpack <code>vue-loader</code> ，会在开发环境下编译模板</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> compiler = <span class="built_in">require</span>(<span class="string">&#x27;vue-template-compiler&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">// *插值</span></span><br><span class="line"><span class="keyword">const</span> template = <span class="string">`&lt;p&gt;&#123;&#123;message&#125;&#125;&lt;/p&gt;`</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* with (this) &#123;</span></span><br><span class="line"><span class="comment">  return createElement(&#x27;p&#x27;, [createTextVNode(toString(message))])</span></span><br><span class="line"><span class="comment">&#125; */</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 编译</span></span><br><span class="line"><span class="keyword">const</span> res = compiler.compile(template)</span><br><span class="line"><span class="built_in">console</span>.log(res.render)</span><br></pre></td></tr></table></figure>

<ul>
<li>三元运算符</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// *三元运算符</span></span><br><span class="line"><span class="keyword">const</span> template = <span class="string">`&lt;p&gt;&#123;&#123;flag ? message : &#x27;no message found&#x27;&#125;&#125;&lt;/p&gt;`</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">with</span> (<span class="built_in">this</span>) &#123;</span><br><span class="line">  <span class="keyword">return</span> createElement(<span class="string">&#x27;p&#x27;</span>, [createTextVNode(toString(flag ? message : <span class="string">&#x27;no message found&#x27;</span>))])</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>有 <code>class、id</code> 属性和动态属性</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// *属性和动态属性</span></span><br><span class="line"><span class="keyword">const</span> template = <span class="string">`</span></span><br><span class="line"><span class="string">  &lt;div id=&quot;div1&quot; class=&quot;container&quot;&gt;</span></span><br><span class="line"><span class="string">    &lt;img :src=&quot;imgUrl&quot;/&gt;</span></span><br><span class="line"><span class="string">  &lt;/div&gt;`</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">with</span> (<span class="built_in">this</span>) &#123;</span><br><span class="line">  <span class="keyword">return</span> createElement(<span class="string">&#x27;div&#x27;</span>, &#123; <span class="attr">staticClass</span>: <span class="string">&#x27;container&#x27;</span>, <span class="attr">attrs</span>: &#123; <span class="attr">id</span>: <span class="string">&#x27;div1&#x27;</span> &#125; &#125;, [</span><br><span class="line">    createElement(<span class="string">&#x27;img&#x27;</span>, &#123; <span class="attr">attrs</span>: &#123; <span class="attr">src</span>: imgUrl &#125; &#125;),</span><br><span class="line">  ])</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li><code>v-if</code> 条件</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// *条件</span></span><br><span class="line"><span class="keyword">const</span> template = <span class="string">`</span></span><br><span class="line"><span class="string">  &lt;div&gt;</span></span><br><span class="line"><span class="string">    &lt;p v-if=&quot;flag === &#x27;a&#x27;&quot;&gt;A&lt;/p&gt;</span></span><br><span class="line"><span class="string">    &lt;p v-else&gt;B&lt;/p&gt;</span></span><br><span class="line"><span class="string">  &lt;/div&gt;`</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">with</span> (<span class="built_in">this</span>) &#123;</span><br><span class="line">  <span class="keyword">return</span> createElement(<span class="string">&#x27;div&#x27;</span>, [</span><br><span class="line">    flag === <span class="string">&#x27;a&#x27;</span></span><br><span class="line">      ? createElement(<span class="string">&#x27;p&#x27;</span>, [createTextVNode(<span class="string">&#x27;A&#x27;</span>)])</span><br><span class="line">      : createElement(<span class="string">&#x27;p&#x27;</span>, [createTextVNode(<span class="string">&#x27;B&#x27;</span>)]),</span><br><span class="line">  ])</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li><code>v-show</code> </li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// *v-show</span></span><br><span class="line"><span class="keyword">const</span> template = <span class="string">`&lt;p v-show=&quot;flag === &#x27;a&#x27;&quot;&gt;A&lt;/p&gt;`</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">with</span> (<span class="built_in">this</span>) &#123;</span><br><span class="line">  <span class="keyword">return</span> createElement(</span><br><span class="line">    <span class="string">&#x27;p&#x27;</span>,</span><br><span class="line">    &#123;</span><br><span class="line">      directives: [</span><br><span class="line">        &#123; <span class="attr">name</span>: <span class="string">&#x27;show&#x27;</span>, <span class="attr">rawName</span>: <span class="string">&#x27;v-show&#x27;</span>, <span class="attr">value</span>: flag === <span class="string">&#x27;a&#x27;</span>, <span class="attr">expression</span>: <span class="string">&quot;flag === &#x27;a&#x27;&quot;</span> &#125;,</span><br><span class="line">      ],</span><br><span class="line">    &#125;,</span><br><span class="line">    [createTextVNode(<span class="string">&#x27;A&#x27;</span>)]</span><br><span class="line">  )</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// v-show 操作的是样式 https://github.com/vuejs/vue/blob/dev/src/platforms/web/runtime/directives/show.js</span></span><br><span class="line">bind (el: any, &#123; value &#125;: VNodeDirective, <span class="attr">vnode</span>: VNodeWithData) &#123;</span><br><span class="line">  vnode = locateNode(vnode)</span><br><span class="line">  <span class="keyword">const</span> transition = vnode.data &amp;&amp; vnode.data.transition</span><br><span class="line">  <span class="keyword">const</span> originalDisplay = el.__vOriginalDisplay =</span><br><span class="line">    el.style.display === <span class="string">&#x27;none&#x27;</span> ? <span class="string">&#x27;&#x27;</span> : el.style.display</span><br><span class="line">  <span class="keyword">if</span> (value &amp;&amp; transition) &#123;</span><br><span class="line">    vnode.data.show = <span class="literal">true</span></span><br><span class="line">    enter(vnode, <span class="function">() =&gt;</span> &#123;</span><br><span class="line">      el.style.display = originalDisplay</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    el.style.display = value ? originalDisplay : <span class="string">&#x27;none&#x27;</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li><code>v-for</code> 循环</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// *循环</span></span><br><span class="line"><span class="keyword">const</span> template = <span class="string">`</span></span><br><span class="line"><span class="string">  &lt;ul&gt;</span></span><br><span class="line"><span class="string">    &lt;li v-for=&quot;item in list&quot; :key=&quot;item.id&quot;&gt;&#123;&#123;item.title&#125;&#125;&lt;/li&gt;</span></span><br><span class="line"><span class="string">  &lt;/ul&gt;`</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">with</span> (<span class="built_in">this</span>) &#123;</span><br><span class="line">  <span class="keyword">return</span> createElement(</span><br><span class="line">    <span class="string">&#x27;ul&#x27;</span>,</span><br><span class="line">    renderList(list, <span class="function"><span class="keyword">function</span>(<span class="params">item</span>) </span>&#123;</span><br><span class="line">      <span class="keyword">return</span> createElement(<span class="string">&#x27;li&#x27;</span>, &#123; <span class="attr">key</span>: item.id &#125;, [createTextVNode(toString(item.title))])</span><br><span class="line">    &#125;),</span><br><span class="line">    <span class="number">0</span></span><br><span class="line">  )</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li><code>event</code> 事件</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// *事件</span></span><br><span class="line"><span class="keyword">const</span> template = <span class="string">`&lt;button @click=&quot;clickHandler&quot;&gt;submit&lt;/button&gt;`</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">with</span> (<span class="built_in">this</span>) &#123;</span><br><span class="line">  <span class="keyword">return</span> createElement(<span class="string">&#x27;button&#x27;</span>, &#123; <span class="attr">on</span>: &#123; <span class="attr">click</span>: clickHandler &#125; &#125;, [createTextVNode(<span class="string">&#x27;submit&#x27;</span>)])</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li><code>v-model</code></li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// *v-model</span></span><br><span class="line"><span class="keyword">const</span> template = <span class="string">`&lt;input type=&quot;text&quot; v-model=&quot;name&quot;&gt;`</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">with</span> (<span class="built_in">this</span>) &#123;</span><br><span class="line">  <span class="keyword">return</span> createElement(<span class="string">&#x27;input&#x27;</span>, &#123;</span><br><span class="line">    directives: [&#123; <span class="attr">name</span>: <span class="string">&#x27;model&#x27;</span>, <span class="attr">rawName</span>: <span class="string">&#x27;v-model&#x27;</span>, <span class="attr">value</span>: name, <span class="attr">expression</span>: <span class="string">&#x27;name&#x27;</span> &#125;],</span><br><span class="line">    attrs: &#123; <span class="attr">type</span>: <span class="string">&#x27;text&#x27;</span> &#125;,</span><br><span class="line">    domProps: &#123; <span class="attr">value</span>: name &#125;,</span><br><span class="line">    on: &#123;</span><br><span class="line">      input: <span class="function"><span class="keyword">function</span>(<span class="params">$event</span>) </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> ($event.target.composing) <span class="keyword">return</span></span><br><span class="line">        name = $event.target.value</span><br><span class="line">      &#125;,</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="vue-组件使用-render-函数"><a href="#vue-组件使用-render-函数" class="headerlink" title="vue 组件使用 render 函数"></a>vue 组件使用 render 函数</h3><ul>
<li>有些复杂情况中，不能用 template，可以考虑用 render</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">Vue.component(<span class="string">&#x27;heading&#x27;</span>, &#123;</span><br><span class="line">  render: <span class="function"><span class="keyword">function</span>(<span class="params">createElement</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> createElement(<span class="string">&#x27;h&#x27;</span> + <span class="built_in">this</span>.level, [</span><br><span class="line">      createElement(</span><br><span class="line">        <span class="string">&#x27;a&#x27;</span>,</span><br><span class="line">        &#123;</span><br><span class="line">          attrs: &#123;</span><br><span class="line">            name: <span class="string">&#x27;headerId&#x27;</span>,</span><br><span class="line">            href: <span class="string">&#x27;#&#x27;</span> + <span class="string">&#x27;headerId&#x27;</span>,</span><br><span class="line">          &#125;,</span><br><span class="line">        &#125;,</span><br><span class="line">        <span class="string">&#x27;this is a tag&#x27;</span></span><br><span class="line">      ),</span><br><span class="line">    ])</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<h2 id="Vue-组件渲染和更新"><a href="#Vue-组件渲染和更新" class="headerlink" title="Vue 组件渲染和更新"></a>Vue 组件渲染和更新</h2><p><strong>初次渲染过程：</strong></p>
<ul>
<li>解析模板为 <code>render</code> 函数（或在开发环境已完成，vue-loader）</li>
<li>触发响应式，监听 <code>data</code> 属性 <code>getter</code> 和 <code>setter</code></li>
<li>执行 <code>render</code> 函数，生成 <code>vnode</code>，<code>patch(elem, vnode)</code></li>
</ul>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">p</span>&gt;</span>&#123;&#123; message &#125;&#125;<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span></span><br><span class="line"><span class="javascript">  <span class="function"><span class="title">data</span>(<span class="params"></span>)</span> &#123;</span></span><br><span class="line"><span class="javascript">    <span class="keyword">return</span> &#123;</span></span><br><span class="line"><span class="javascript">      message: <span class="string">&#x27;hello&#x27;</span>, <span class="comment">// 会触发get</span></span></span><br><span class="line"><span class="javascript">      city: <span class="string">&#x27;北京&#x27;</span>,     <span class="comment">// 不会触发get，因为模板没有用到，即和视图没关系</span></span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p><strong>更新过程：</strong></p>
<ul>
<li>修改 <code>data</code>，触发 <code>setter</code>（此前在 <code>getter</code> 中已被监听）</li>
<li>重新执行 <code>render</code> 函数，生成 <code>newVnode</code></li>
<li><code>patch(vnode, newVnode)</code></li>
</ul>
<p><img data-src="https://gitee.com/lilyn/pic/raw/master/jslearn-img/vue%E6%B8%B2%E6%9F%93%E6%9B%B4%E6%96%B0%E6%B5%81%E7%A8%8B%E5%9B%BE.png"></p>
<ul>
<li>生成 <code>render</code> 函数，其生成一个 <code>vnode</code>，它会 <code>touch</code> 触发 <code>getter</code> 进行收集依赖</li>
<li>在模板中哪个被引用了就会将其用 <code>Watcher</code> 观察起来，发生了 <code>setter</code> 也会将其 <code>Watcher</code> 起来</li>
<li>如果之前已经被 <code>Watcher</code> 观察起来，发生更新进行重新渲染</li>
</ul>
<p><strong>异步渲染</strong></p>
<blockquote>
<p>源码级别可以参考：<a target="_blank" rel="noopener" href="https://ustbhuangyi.github.io/vue-analysis/v2/reactive/next-tick.html">Vue.js 技术揭秘——nextTick</a></p>
</blockquote>
<ul>
<li><code>$nextTick</code></li>
<li>汇总 <code>data</code> 的修改，一次性更新视图</li>
<li>减少 DOM 操作次数，提高性能</li>
</ul>
<h2 id="前端路由原理"><a href="#前端路由原理" class="headerlink" title="前端路由原理"></a>前端路由原理</h2><h3 id="hash-路由"><a href="#hash-路由" class="headerlink" title="hash 路由"></a>hash 路由</h3><p><strong>hash 的特点：</strong></p>
<ul>
<li><code>hash</code> 变化会触发网页跳转，即浏览器的前进、后退</li>
<li><code>hash</code> 变化不会刷新页面，<code>SPA</code> 必需的特点</li>
<li><code>hash</code> 永远不会提交到 <code>server</code> 端（前端自生自灭）</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">http:<span class="comment">//127.0.0.1:5500/src/1.html?a=100&amp;b=20#/aaa/bbb/</span></span><br><span class="line">location.protocol <span class="comment">// &#x27;http:&#x27;</span></span><br><span class="line">location.hostname <span class="comment">// &#x27;127.0.0.1&#x27;</span></span><br><span class="line">location.host <span class="comment">// &#x27;127.0.0.1:5500&#x27;</span></span><br><span class="line">location.port <span class="comment">// &#x27;5500&#x27;</span></span><br><span class="line">location.pathname <span class="comment">// &#x27;/src/1.html&#x27;</span></span><br><span class="line">location.search <span class="comment">// &#x27;?a=100&amp;b=20&#x27;</span></span><br><span class="line">location.hash <span class="comment">// &#x27;#/aaa/bbb/&#x27;</span></span><br></pre></td></tr></table></figure>

<p><strong>hash 变化：</strong></p>
<ol>
<li>JS 修改 <code>url</code></li>
<li>手动修改 <code>url</code> 的 <code>hash</code></li>
<li>浏览器前进、后退</li>
</ol>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">p</span>&gt;</span>hash test<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;btn1&quot;</span>&gt;</span>修改 hash<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript">  <span class="built_in">window</span>.onhashchange = <span class="function"><span class="params">event</span> =&gt;</span> &#123;</span></span><br><span class="line"><span class="javascript">    <span class="built_in">console</span>.log(<span class="string">&#x27;old url&#x27;</span>, event.oldURL)</span></span><br><span class="line"><span class="javascript">    <span class="built_in">console</span>.log(<span class="string">&#x27;new url&#x27;</span>, event.newURL)</span></span><br><span class="line"></span><br><span class="line"><span class="javascript">    <span class="built_in">console</span>.log(<span class="string">&#x27;hash:&#x27;</span>, location.hash)</span></span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line"><span class="javascript">  <span class="comment">// 页面初次加载，获取 hash</span></span></span><br><span class="line"><span class="javascript">  <span class="built_in">document</span>.addEventListener(<span class="string">&#x27;DOMContentLoaded&#x27;</span>, <span class="function">() =&gt;</span> &#123;</span></span><br><span class="line"><span class="javascript">    <span class="built_in">console</span>.log(<span class="string">&#x27;hash:&#x27;</span>, location.hash)</span></span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line"><span class="javascript">  <span class="comment">// JS 修改 url</span></span></span><br><span class="line"><span class="javascript">  <span class="built_in">document</span>.getElementById(<span class="string">&#x27;btn1&#x27;</span>).addEventListener(<span class="string">&#x27;click&#x27;</span>, <span class="function">() =&gt;</span> &#123;</span></span><br><span class="line"><span class="javascript">    location.href = <span class="string">&#x27;#/user&#x27;</span></span></span><br><span class="line">  &#125;)</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="history-路由"><a href="#history-路由" class="headerlink" title="history 路由"></a>history 路由</h3><ul>
<li>用 <code>url</code> 规范的路由，但跳转时不刷新页面</li>
<li>需要 <code>server</code> 端配合，可参考：<a target="_blank" rel="noopener" href="https://router.vuejs.org/zh/guide/essentials/history-mode.html#%E5%90%8E%E7%AB%AF%E9%85%8D%E7%BD%AE%E4%BE%8B%E5%AD%90">后端配置例子</a></li>
<li><code>pushState</code> 不会触发 <code>hashchange</code> 事件，<code>popstate</code> 事件只会在浏览器某些行为下触发，比如点击后退、前进按钮</li>
</ul>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">p</span>&gt;</span>history API test<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;btn1&quot;</span>&gt;</span>修改 url<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript">  <span class="comment">// 页面初次加载，获取 path</span></span></span><br><span class="line"><span class="javascript">  <span class="built_in">document</span>.addEventListener(<span class="string">&#x27;DOMContentLoaded&#x27;</span>, <span class="function">() =&gt;</span> &#123;</span></span><br><span class="line"><span class="javascript">    <span class="built_in">console</span>.log(<span class="string">&#x27;load&#x27;</span>, location.pathname)</span></span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line"><span class="javascript">  <span class="comment">// 打开一个新的路由 【注意】用 pushState 方式，浏览器不会刷新页面</span></span></span><br><span class="line"><span class="javascript">  <span class="built_in">document</span>.getElementById(<span class="string">&#x27;btn1&#x27;</span>).addEventListener(<span class="string">&#x27;click&#x27;</span>, <span class="function">() =&gt;</span> &#123;</span></span><br><span class="line"><span class="javascript">    <span class="keyword">const</span> state = &#123; <span class="attr">name</span>: <span class="string">&#x27;page1&#x27;</span> &#125;</span></span><br><span class="line"><span class="javascript">    <span class="built_in">console</span>.log(<span class="string">&#x27;切换路由到&#x27;</span>, <span class="string">&#x27;page1&#x27;</span>)</span></span><br><span class="line"><span class="javascript">    history.pushState(state, <span class="string">&#x27;&#x27;</span>, <span class="string">&#x27;page1&#x27;</span>)</span></span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line"><span class="javascript">  <span class="comment">// 监听浏览器前进、后退</span></span></span><br><span class="line"><span class="javascript">  <span class="built_in">window</span>.onpopstate = <span class="function"><span class="params">event</span> =&gt;</span> &#123;</span></span><br><span class="line"><span class="javascript">    <span class="built_in">console</span>.log(<span class="string">&#x27;onpopstate&#x27;</span>, event.state, location.pathname)</span></span><br><span class="line">  &#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p><strong>两者选择</strong></p>
<ul>
<li><code>to B</code> 的系统推荐用 <code>hash</code>，简单易用，对 <code>url</code> 规范不敏感</li>
<li><code>to C</code> 的系统，可以考虑选择 <code>H5 history</code> ，但需要服务端支持</li>
<li>能选择简单的，就别用复杂的，要考虑成本和收益</li>
</ul>

    </div>

    
    
    

	<div>
		  
			
<div class="my_post_copyright clearfix">
  <script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script>
  
  <!-- JS库 sweetalert 可修改路径 -->
  <script src="https://cdn.bootcss.com/jquery/2.0.0/jquery.min.js"></script>
  <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
  <article class="box-left">
    <img src="https://gitee.com/lilyn/pic/raw/master/logo.jpg">
  </article>
  <article class="box-right">
    <p><span>本文标题:</span><a href="/posts/65e0f6f9/">Vue 原理（虚拟DOM、diff算法、模板编译）</a></p>
    <p><span>文章作者:</span><a href="/" title="访问 LiLyn 的个人博客">LiLyn</a></p>
    <p><span>原始链接:</span><a href="/posts/65e0f6f9/" title="Vue 原理（虚拟DOM、diff算法、模板编译）">http://llmysnow.top/posts/65e0f6f9/</a>
    <span class="copy-path"  title="点击复制文章链接"><i class="fa fa-clipboard" data-clipboard-text="http://llmysnow.top/posts/65e0f6f9/"  aria-label="复制成功！"></i></span>
  </p>
    <p><span>许可协议:</span>转载请保留原文链接及作者。</p>  
  </article>
</div>
<script> 
    var clipboard = new Clipboard('.fa-clipboard');
    $(".fa-clipboard").click(function(){
      clipboard.on('success', function(){
        swal({   
          title: "",   
          text: '复制成功',
          icon: "success", 
          showConfirmButton: true
          });
    });
    });  
</script>

		  
	</div>

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Vue/" rel="tag"><i class="fa fa-tag"></i> Vue</a>
              <a href="/tags/diff%E7%AE%97%E6%B3%95/" rel="tag"><i class="fa fa-tag"></i> diff算法</a>
              <a href="/tags/%E8%99%9A%E6%8B%9FDOM/" rel="tag"><i class="fa fa-tag"></i> 虚拟DOM</a>
              <a href="/tags/%E6%A8%A1%E6%9D%BF%E7%BC%96%E8%AF%91/" rel="tag"><i class="fa fa-tag"></i> 模板编译</a>
              <a href="/tags/%E5%89%8D%E7%AB%AF%E8%B7%AF%E7%94%B1%E5%8E%9F%E7%90%86/" rel="tag"><i class="fa fa-tag"></i> 前端路由原理</a>
          </div>
		 
		<!--post-tags 彩色标签-->
		<script type="text/javascript">
		var tagsall=document.getElementsByClassName("post-tags")
		for (var i = tagsall.length - 1; i >= 0; i--){
			var tags=tagsall[i].getElementsByTagName("a");
			for (var j = tags.length - 1; j >= 0; j--) {
				var r=Math.floor(Math.random()*75+130);
				var g=Math.floor(Math.random()*75+100);
				var b=Math.floor(Math.random()*75+80);
				tags[j].style.background = "rgb("+r+","+g+","+b+")";
				tags[j].style.color = "#fff";
				tags[j].style.padding = "0 10px";
			}
		}                        
		</script>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/posts/5381f975/" rel="prev" title="Vue3 面试题">
      <i class="fa fa-chevron-left"></i> Vue3 面试题
    </a></div>
      <div class="post-nav-item">
    <a href="/posts/167dee54/" rel="next" title="nvm 搭建 node 环境">
      nvm 搭建 node 环境 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#Vue-%E5%8E%9F%E7%90%86"><span class="nav-text">Vue 原理</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%A6%82%E4%BD%95%E7%90%86%E8%A7%A3-MVVM"><span class="nav-text">如何理解 MVVM</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Vue-%E5%93%8D%E5%BA%94%E5%BC%8F%EF%BC%88%E6%95%B0%E6%8D%AE%E6%8B%A6%E6%88%AA%EF%BC%89"><span class="nav-text">Vue 响应式（数据拦截）</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%95%B0%E6%8D%AE%E5%93%8D%E5%BA%94%E5%BC%8F%E5%8E%9F%E7%90%86"><span class="nav-text">数据响应式原理</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%99%9A%E6%8B%9F-DOM"><span class="nav-text">虚拟 DOM</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#vdom"><span class="nav-text">vdom</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#snabbdom-%E4%BD%BF%E7%94%A8"><span class="nav-text">snabbdom 使用</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#diff-%E7%AE%97%E6%B3%95"><span class="nav-text">diff 算法</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#snabbdom-%E6%BA%90%E7%A0%81"><span class="nav-text">snabbdom 源码</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#patchVnode"><span class="nav-text">patchVnode</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#addVnodes-removeVnodes"><span class="nav-text">addVnodes removeVnodes</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#updateChildren"><span class="nav-text">updateChildren</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%80%BB%E7%BB%93"><span class="nav-text">总结</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%A8%A1%E6%9D%BF%E7%BC%96%E8%AF%91"><span class="nav-text">模板编译</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#vue-%E6%A8%A1%E6%9D%BF%E8%A2%AB%E7%BC%96%E8%AF%91%E6%88%90%E4%BB%80%E4%B9%88"><span class="nav-text">vue 模板被编译成什么</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#vue-%E7%BB%84%E4%BB%B6%E4%BD%BF%E7%94%A8-render-%E5%87%BD%E6%95%B0"><span class="nav-text">vue 组件使用 render 函数</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Vue-%E7%BB%84%E4%BB%B6%E6%B8%B2%E6%9F%93%E5%92%8C%E6%9B%B4%E6%96%B0"><span class="nav-text">Vue 组件渲染和更新</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%89%8D%E7%AB%AF%E8%B7%AF%E7%94%B1%E5%8E%9F%E7%90%86"><span class="nav-text">前端路由原理</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#hash-%E8%B7%AF%E7%94%B1"><span class="nav-text">hash 路由</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#history-%E8%B7%AF%E7%94%B1"><span class="nav-text">history 路由</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="LiLyn"
      src="/images/logo.jpg">
  <p class="site-author-name" itemprop="name">LiLyn</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">70</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">10</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">162</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/llwodexue" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;llwodexue" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://blog.csdn.net/qq_38689395" title="CSDN → https:&#x2F;&#x2F;blog.csdn.net&#x2F;qq_38689395" rel="noopener" target="_blank"><i class="fas fa-copyright fa-fw"></i>CSDN</a>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title"><i class="fa fa-link fa-fw"></i>
      友情链接
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="https://scx69.gitee.io/blogs/" title="https:&#x2F;&#x2F;scx69.gitee.io&#x2F;blogs&#x2F;" rel="noopener" target="_blank">花开满城</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="http://yixin-oss.gitee.io/daytoy/" title="http:&#x2F;&#x2F;yixin-oss.gitee.io&#x2F;daytoy&#x2F;" rel="noopener" target="_blank">daytoy</a>
        </li>
    </ul>
  </div>

      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">LiLyn</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
    <span title="站点总字数">463k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">7:01</span>
</div>

<!--
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>-->

<!--
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<div class="powered-by">
  <i class="fa fa-user-md"></i>
  <span id="busuanzi_container_site_pv">
    本站访问量:<span id="busuanzi_value_site_pv"></span>次
  </span>
  <span class="post-meta-divider">|</span>
  <span id="busuanzi_container_site_uv">
    本站总访客量：<span id="busuanzi_value_site_uv"></span>人
  </span>
</div>-->

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/lozad@1/dist/lozad.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  
  <script>
    (function(){
      var canonicalURL, curProtocol;
      //Get the <link> tag
      var x=document.getElementsByTagName("link");
		//Find the last canonical URL
		if(x.length > 0){
			for (i=0;i<x.length;i++){
				if(x[i].rel.toLowerCase() == 'canonical' && x[i].href){
					canonicalURL=x[i].href;
				}
			}
		}
    //Get protocol
	    if (!canonicalURL){
	    	curProtocol = window.location.protocol.split(':')[0];
	    }
	    else{
	    	curProtocol = canonicalURL.split(':')[0];
	    }
      //Get current URL if the canonical URL does not exist
	    if (!canonicalURL) canonicalURL = window.location.href;
	    //Assign script content. Replace current URL with the canonical URL
      !function(){var e=/([http|https]:\/\/[a-zA-Z0-9\_\.]+\.baidu\.com)/gi,r=canonicalURL,t=document.referrer;if(!e.test(r)){var n=(String(curProtocol).toLowerCase() === 'https')?"https://sp0.baidu.com/9_Q4simg2RQJ8t7jm9iCKT-xh_/s.gif":"//api.share.baidu.com/s.gif";t?(n+="?r="+encodeURIComponent(document.referrer),r&&(n+="&l="+r)):r&&(n+="?l="+r);var i=new Image;i.src=n}}(window);})();
  </script>




  
<script src="/js/local-search.js"></script>













  

  

  <!-- 动态线条背景 -->
  <script type="text/javascript" src="//cdn.bootcss.com/canvas-nest.js/1.0.0/canvas-nest.min.js"></script>
  <!-- 页面点击小红心 -->
  <script type="text/javascript">
	!function(e,t,a){function n(){c(".heart{width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: fixed;}.heart:after{top: -5px;}.heart:before{left: -5px;}"),o(),r()}function r(){for(var e=0;e<d.length;e++)d[e].alpha<=0?(t.body.removeChild(d[e].el),d.splice(e,1)):(d[e].y--,d[e].scale+=.004,d[e].alpha-=.013,d[e].el.style.cssText="left:"+d[e].x+"px;top:"+d[e].y+"px;opacity:"+d[e].alpha+";transform:scale("+d[e].scale+","+d[e].scale+") rotate(45deg);background:"+d[e].color+";z-index:99999");requestAnimationFrame(r)}function o(){var t="function"==typeof e.onclick&&e.onclick;e.onclick=function(e){t&&t(),i(e)}}function i(e){var a=t.createElement("div");a.className="heart",d.push({el:a,x:e.clientX-5,y:e.clientY-5,scale:1,alpha:1,color:s()}),t.body.appendChild(a)}function c(e){var a=t.createElement("style");a.type="text/css";try{a.appendChild(t.createTextNode(e))}catch(t){a.styleSheet.cssText=e}t.getElementsByTagName("head")[0].appendChild(a)}function s(){return"rgb("+~~(255*Math.random())+","+~~(255*Math.random())+","+~~(255*Math.random())+")"}var d=[];e.requestAnimationFrame=function(){return e.requestAnimationFrame||e.webkitRequestAnimationFrame||e.mozRequestAnimationFrame||e.oRequestAnimationFrame||e.msRequestAnimationFrame||function(e){setTimeout(e,1e3/60)}}(),n()}(window,document);
  </script>
<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"log":false,"model":{"jsonPath":"/live2dw/assets/hijiki.model.json"},"display":{"position":"right","width":150,"height":300},"mobile":{"show":true},"react":{"opacity":0.7}});</script></body>
</html>